/***************************************************************************
*   Copyright (C) 2005 by Alexandr Nemish   *
*   atlanter@gmail.com   *
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
*   This program is distributed in the hope that it will be useful,       *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
*   GNU General Public License for more details.                          *
*                                                                         *
*   You should have received a copy of the GNU General Public License     *
*   along with this program; if not, write to the                         *
*   Free Software Foundation, Inc.,                                       *
*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/
#ifndef RENDERER_H
#define RENDERER_H

#include <qstringlist.h>
#include <qfont.h>
#include <qstring.h>
#include <qobject.h>
#include <qsize.h>
#include <vector>
class QStringList;

class Paragraph
{
public:
	Paragraph();
	Paragraph(const QString & a_str);
	Paragraph(const QString & a_str, long a_width);
	const QString & text() const { return m_text; }
	long width() const { return m_width; }
private:
	QString m_text;
	long	m_width;
};


/**
*   @brief Renders input text into list of paragraphs
*   @author Alexandr Nemish <atlanter@gmail.com>
*/
class Renderer : public QObject
{
	Q_OBJECT
public:
	Renderer();
	~Renderer();

	void load(const QStringList & list);
	void render();
	void drawPage(QPainter & paint, QRect rect, int pageNumber);
	//Getters
	bool isEmpty() const { return m_empty; };
	QFont font() const { return m_font; }
	int pageCount() const { return m_pageCount; }
	QSize pageSize() const { return m_pageSize; }
	//Setters
	void setFont(const QFont & font);
	void setPageSize(const QSize & size);

signals:
	void loaded(int);

private:
	typedef QStringList::const_iterator TStringIter;
	typedef std::vector<Paragraph*>::iterator TParagsIter;
			
	Renderer(const Renderer &);
	Renderer & operator = (const Renderer &);
	void drawLine(QPainter& paint, int x, int y, const QString&);
	void parseString(const QString & string);
	void addLine(const QString & a_string);
	QString getWord(const QString & str, int idx);
	int wordAt(const QString & str, int len);
	int width(const QString & a_string) const;

	QStringList m_text;
	QStringList m_lines;
	QFont m_font;
	QSize m_pageSize;
	int m_pageCount;
	int m_linesPerPage;
	bool m_empty;
	bool m_isStartAdded;
	
	std::vector<Paragraph*>	m_parags;
};

#endif
